package org.tissou.client.Panel_Premier.bilan_tab.Affichages_De_Bilan;

import java.util.Date;

import org.tissou.client.DonneesAAfficher.Donnees_Base;
import org.tissou.client.DonneesAAfficher.Element_Tiers;
import org.tissou.client.DonneesAAfficher.ParametresQuery;
import org.tissou.client.DonneesAAfficher.RecuperateurAfficheurDonnees;
import org.tissou.client.DonneesAAfficher.Tableau;
import org.tissou.client.DonneesAAfficher.TypeDonnee;
import org.tissou.client.Interface.Base_Panel;
import org.tissou.client.Services.ArbreDeCategories;
import org.tissou.client.Services.ArbreDeMonnaies;
import org.tissou.client.Services.CategorieInfos;
import org.tissou.client.Services.InformationsGlobales;
import org.tissou.client.Services.MonException;
import org.tissou.client.Services.MonnaieInfos;
import org.tissou.client.Services.ProprietaireInfos;
import org.tissou.client.widgets.Label_MessageInformation;
import org.tissou.client.widgets.Label_MessageInformation.Etat;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.TreeListener;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class Bilan_General extends Base_Panel {

	  DockPanel principal = new DockPanel();
	  
	  DockPanel panel_monnaiesdate = new DockPanel();

	  DockPanel panel_monnaies = new DockPanel();
	  Label label_monnaies = new Label("Choisissez votre monnaie");
	  ArbreDeMonnaies arbremonnaies = new ArbreDeMonnaies();
	  MonnaieInfos monnaie;
	  Boolean monnaieselectionnee = false;

	  DockPanel panel_dates = new DockPanel();
	  DateBox dateInitialPicker = new DateBox();
	  Label dateInitiale_Label = new Label("De (inclus) : ");
	  DateBox dateFinalePicker = new DateBox();
	  Label dateFinale_Label = new Label("A (inclus) : ");
	  
	  DockPanel panel_categories = new DockPanel();
	  Label label_categories = new Label("Choisissez le categorie mère");
	  ArbreDeCategories arbrecategories = new ArbreDeCategories();
	  CategorieInfos categorie;
	  Boolean categorieselectionnee = false;
	  
	  Button ok = new Button();
	  
	  DockPanel panel_resultat = new DockPanel();
	  DockPanel panel_bilan = new DockPanel();
	  Label label_bilantotal = new Label("Le bilan total est :");
	  DockPanel panel_bilantotal = new DockPanel();
	  
	  Label_MessageInformation message_information = new Label_MessageInformation();
	  Label_MessageInformation message_information2 = new Label_MessageInformation();

	  protected void MiseEnPlaceStyles() {
	    panel_monnaies.addStyleName("dockPanelMonnaies");
	    panel_categories.addStyleName("dockPanelCategories");
	    panel_bilan.addStyleName("dockPanelDettes");
	    panel_dates.addStyleName("dockPanelcellulesimple");
	  }

	  protected void MiseEnPlaceParametresWidgets() {
		  ok.setText("OK");

	  }

	  protected void MiseEnPlaceParametresPanel() {
	    panel_monnaies.add(label_monnaies, DockPanel.NORTH);
	    panel_monnaies.add(arbremonnaies, DockPanel.NORTH);
	    panel_categories.add(label_categories, DockPanel.NORTH);
	    panel_categories.add(arbrecategories, DockPanel.NORTH);
	    panel_dates.add(dateInitiale_Label, DockPanel.NORTH);
	    panel_dates.add(dateInitialPicker, DockPanel.NORTH);
	    panel_dates.add(dateFinale_Label, DockPanel.NORTH);
	    panel_dates.add(dateFinalePicker, DockPanel.NORTH);
	  }

	  protected void MiseEnPlaceActions() {
		
		ok.addClickListener(new ClickListener(){

			@Override
			public void onClick(Widget sender) {
				if (monnaieselectionnee&&categorieselectionnee){
			          AfficherLeBilanMensuel(monnaie, categorie, dateInitialPicker.getValue(), dateFinalePicker.getValue());
			        }
				
			}
			
		});
		  
	    arbremonnaies.addTreeListener(new TreeListener(){
	      public void onTreeItemSelected(TreeItem item) {

	        try { monnaie = arbremonnaies.donneLaMonnaieSelectionnee();
	        } catch (MonException e){
	          message_information.setMessage(Label_MessageInformation.Etat.MAUVAISEENTREE, e.getMessage());
	          throw e;
	        }

	        monnaieselectionnee = true;
	      }

	      public void onTreeItemStateChanged(TreeItem item) {

	      }
	    });

	    arbrecategories.addTreeListener(new TreeListener(){

	      public void onTreeItemSelected(TreeItem item) {
	        categorie = arbrecategories.donneLaCategorieSelectionnee();
	        categorieselectionnee = true;
	      }

	      public void onTreeItemStateChanged(TreeItem item) {
	      }
	    }
	    );
	  }

	  private void AfficherLeBilanMensuel(MonnaieInfos monnaie, CategorieInfos categorie, Date dateInitiale, Date dateFinale){
		  
		if (dateInitiale.after(dateFinale)){
			message_information.setMessage(Etat.MAUVAISEENTREE, "La date Finale est antérieure à la date Initiale !");
		}
		else {  
		    ParametresQuery parametres = new ParametresQuery.Builder()
		        .addProprietaires(InformationsGlobales.personneLoguee)
		        .addCategorie(categorie)
		        .addMonnaie(monnaie)
		        .addDate(dateInitiale)
		        .addDate(dateFinale)	    
		        .setType(TypeDonnee.BILANGENERAL)
		        .build();
	
		    panel_bilan.clear();
	
		    RecuperateurAfficheurDonnees recuperateur = new RecuperateurAfficheurDonnees(parametres, panel_bilan,
		        message_information);
		    recuperateur.recupererEtAfficherDonnees();

		    panel_bilantotal.clear();
		    
		    RecuperateurAfficheurDonnees recuperateur2 = new RecuperateurAfficheurDonnees(ParametresQuery.genererNouveauxParametresChangerTypeDonnee(parametres, TypeDonnee.BILANGENERALTOTAL), panel_bilantotal,
			        message_information2);
			recuperateur2.recupererEtAfficherDonnees();
		    
    
		}
	  }

	  protected void MiseEnPlacePanels() {
	    principal.add(message_information, DockPanel.NORTH);
	    principal.setCellHeight(message_information, "40px");
	    principal.setCellHorizontalAlignment(message_information,HasHorizontalAlignment.ALIGN_CENTER);
	    
	    principal.add(message_information2, DockPanel.NORTH);
	    principal.setCellHeight(message_information2, "40px");
	    principal.setCellHorizontalAlignment(message_information2,HasHorizontalAlignment.ALIGN_CENTER);
	    	    
	    principal.add(panel_monnaiesdate, DockPanel.WEST);
	    principal.setCellWidth(panel_monnaiesdate, "175px");
	    principal.setCellHorizontalAlignment(panel_monnaiesdate, HasHorizontalAlignment.ALIGN_LEFT);
	    panel_monnaiesdate.add(panel_monnaies, DockPanel.NORTH);
	    panel_monnaiesdate.setCellWidth(panel_monnaies, "175px");
	    panel_monnaiesdate.setCellHorizontalAlignment(panel_monnaies, HasHorizontalAlignment.ALIGN_LEFT);
	    panel_monnaiesdate.add(panel_dates, DockPanel.NORTH);
	    panel_monnaiesdate.setCellWidth(panel_dates, "175px");
	    panel_monnaiesdate.setCellHorizontalAlignment(panel_monnaies, HasHorizontalAlignment.ALIGN_LEFT);
	    
	    principal.add(panel_categories, DockPanel.WEST);
	    principal.setCellWidth(panel_categories, "250px");
	    principal.setCellHorizontalAlignment(panel_categories, HasHorizontalAlignment.ALIGN_LEFT);
	   
	    principal.add(panel_resultat, DockPanel.WEST);
	    principal.setCellWidth(panel_resultat, "635px");
	    principal.setCellHorizontalAlignment(panel_resultat, HasHorizontalAlignment.ALIGN_LEFT);
	    panel_resultat.add(ok, DockPanel.NORTH);
	    
	    panel_resultat.add(label_bilantotal, DockPanel.WEST);
	    panel_resultat.setCellWidth(label_bilantotal, "250px");
	    
	    panel_resultat.add(panel_bilantotal, DockPanel.NORTH);
	    panel_resultat.setCellWidth(panel_bilantotal, "635px");
	    panel_resultat.setCellHorizontalAlignment(panel_bilantotal, HasHorizontalAlignment.ALIGN_LEFT);
	       
	    panel_resultat.add(panel_bilan, DockPanel.NORTH);
	    panel_resultat.setCellWidth(panel_bilan, "635px");
	    panel_resultat.setCellHorizontalAlignment(panel_bilan, HasHorizontalAlignment.ALIGN_LEFT);
	  }

	  protected Panel donneLePanelPrincipal() {
	    return principal;  
	  }

}
